package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.YqTyc;
import cn.yuanqiao.archive.appManager.mapper.YqTycMapper;
import cn.yuanqiao.archive.appManager.service.IYqTycService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 同义词Service业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-14
 */
@Service
public class YqTycServiceImpl implements IYqTycService
{
    @Autowired
    private YqTycMapper yqTycMapper;

    /**
     * 查询同义词
     * 
     * @param id 同义词主键
     * @return 同义词
     */
    @Override
    public YqTyc selectYqTycByID(Long id)
    {
        YqTyc yqTyc = new YqTyc();
        yqTyc.setId(id);
        return yqTycMapper.selectYqTycByID(yqTyc);
    }

    /**
     * 查询同义词列表（分页）
     * 
     * @param yqTyc 同义词
     * @return 同义词
     */
    @Override
    public List<YqTyc> selectYqTycList(YqTyc yqTyc)
    {
        return yqTycMapper.selectYqTycList(yqTyc);
    }

    /**
     * 查询同义词列表（全部）
     *
     * @param primary_word_array 同义词
     * @return 同义词
     */
    @Override
    public List<YqTyc> selectYqTycListAll(String[] primary_word_array)
    {
        String yqtycCode = SecurityUtils.getTenantCode();
        String tableName = yqtycCode + "_YQTYC";
        return yqTycMapper.selectYqTycListAll(tableName,primary_word_array);
    }

    /**
     * 新增同义词
     * 
     * @param yqTyc 同义词
     * @return 结果
     */
    @Override
    public String insertYqTyc(YqTyc yqTyc)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        if (yqTycMapper.isOnly(yqTyc)!=0){
//            AjaxResult.error("原词：”"+yqTyc.getprimary_word()+"”已存在！");
            successMsg.append("原词：”"+yqTyc.getPrimary_word()+"”已存在！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

//            if(user == null){
//                user = new LoginUser();
//                user.setUserId(1L);
//            }

            yqTyc.setCreator_id(user.getUserId());
            yqTyc.setCreate_time(DateUtils.getNowDate());
            if(yqTycMapper.insertYqTyc(yqTyc)>0){
                successMsg.append("添加成功！");
            };
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 修改同义词
     * 
     * @param yqTyc 同义词
     * @return 结果
     */
    @Override
    public String updateYqTyc(YqTyc yqTyc)
    {
        int num=0;
        StringBuilder successMsg = new StringBuilder();
        if (yqTycMapper.isOnly(yqTyc)!=0){
//            AjaxResult.error("原词：”"+yqTyc.getprimary_word()+"”已存在！");
            successMsg.append("原词：”"+yqTyc.getPrimary_word()+"”已存在！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

//            if(user == null){
//                user = new LoginUser();
//                user.setUserId(1L);
//            }

            yqTyc.setLastupdater(user.getUserId());
            yqTyc.setLastupdate_time(DateUtils.getNowDate());
            if (yqTycMapper.updateYqTyc(yqTyc)>=0){
                successMsg.append("修改成功！");
            };
        }
//        return num;
        return successMsg.toString();
    }

    /**
     * 修改同义词启用状态
     *
     * @param yqTyc 同义词
     * @return 结果
     */
    @Override
    public int updateYqTycStatus(YqTyc yqTyc)
    {
        return yqTycMapper.updateYqTycStatus(yqTyc);
    }

    /**
     * 批量删除同义词
     * 
     * @param ids 需要删除的同义词主键
     * @return 结果
     */
    @Override
    public int deleteYqTycByIDs(Long[] ids)
    {
        YqTyc yqTyc = new YqTyc();
        return yqTycMapper.deleteYqTycByIDs(yqTyc.getTableName(),ids);
    }

    /**
     * 删除同义词信息
     * 
     * @param id 同义词主键
     * @return 结果
     */
    @Override
    public int deleteYqTycByID(Long id)
    {
        return yqTycMapper.deleteYqTycByID(id);
    }

    /**
     * 导出同义词列表
     *
     * @param ids 同义词
     * @return 同义词
     */
    @Override
    public List<YqTyc> exportYqTycListByIDs(Long[] ids)
    {
        YqTyc yqTyc = new YqTyc();
        return yqTycMapper.exportYqTycListByIDs(yqTyc.getTableName(),ids);
    }

    @Override
    @Transactional
    public AjaxResult importYQTYC(List<YqTyc> tycLists, Boolean isUpdateSupport) {
        StringBuffer msg = new StringBuffer();
        int num = 0;
        for(YqTyc yqTyc:tycLists){
            if (yqTycMapper.isOnly(yqTyc)!=0){
                if (isUpdateSupport){
                    num += yqTycMapper.updateYqTycByWord(yqTyc);
                }else {
                    msg.append("原词：”"+yqTyc.getPrimary_word()+"”已存在！");
                }
            }else {
                LoginUser user = SecurityUtils.getLoginUser();

//                if(user == null){
//                    user = new LoginUser();
//                    user.setUserId(1L);
//                }

                yqTyc.setCreator_id(user.getUserId());
                yqTyc.setCreate_time(DateUtils.getNowDate());
                num += yqTycMapper.insertYqTyc(yqTyc);
            }
        }
        if (num<=0){
            return AjaxResult.error("导入失败！"+msg.toString());
        }else {
            if (msg!= null && msg.length() > 0){
                return AjaxResult.success("成功"+num+"条，失败"+(tycLists.size()-num)+"条。失败原因："+msg.toString());
            }else {
                return AjaxResult.success();
            }

        }
    }
}
